package com.myway.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.myway.dao.FlyFrequentPassengerMapper;
import com.myway.dao.FlyPassengeMapper;
import com.myway.dto.common.JsonData;
import com.myway.dto.common.ResultEntity;
import com.myway.dto.util.IDUtils;
import com.myway.pojo.FlyFrequentPassenger;
import com.myway.utils.UserUtils;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.myway.dto.domain.PassengeResult;
import com.myway.pojo.FlyPassenge;
import com.myway.service.PassengerService;

@Service
@Slf4j
public class PassengerServiceImpl implements PassengerService {
    @Autowired
    private FlyPassengeMapper flyPassengeMapper;
    @Autowired
    private FlyFrequentPassengerMapper flyFrequentPassengerMapper;

    @Override
    public JsonData addPassenger() {
        return null;
    }

    /**
     * 查询常用旅客信息
     * @return
     */
    @Override
    public JsonData searchPassengeList() {
        String userId = UserUtils.getUserId();
        List<PassengeResult> results = new ArrayList<PassengeResult>();
        List<FlyFrequentPassenger> flyPassenges = flyFrequentPassengerMapper.selectPassenge(userId,null);
        for (int i=0;i<flyPassenges.size();i++){
            PassengeResult PResult = new PassengeResult();
            PResult.setId(flyPassenges.get(i).getUserId());
            PResult.setFlyPassenges(flyPassenges.get(i));
            results.add(PResult);
        }
        log.info("常用乘客信息====="+ JSON.toJSON(results));
        return JsonData.success(results);
    }

    @Override
    public JsonData searchPassengeOne(Long id) {
        FlyFrequentPassenger flyPassenge = flyFrequentPassengerMapper.selectByPrimaryKey(id);
        return JsonData.success(flyPassenge);
}

    /**
     * 根据登录人查询添加的乘客信息
     * @param userId
     * @param flag
     * @return
     */
    @Override
    public ResultEntity getPassengerByUserId(String userId, String flag) {
        if (StringUtils.isEmpty(userId)){
            return ResultEntity.fail("User Id Can't Be Null");
        }
        List<FlyFrequentPassenger> passengeList = flyFrequentPassengerMapper.selectPassenge(userId,flag);
        if(passengeList==null){
            return  ResultEntity.fail("Get Passenger Info failed");
        }
        for (int i = 0; i <passengeList.size() ; i++) {
            FlyFrequentPassenger flyPassenge = passengeList.get(i);
            String xingE = flyPassenge.getXingE() == null ? "" : flyPassenge.getXingE();
            String mingE = flyPassenge.getMingE() == null ? "" : flyPassenge.getMingE();
            flyPassenge.setName(mingE+"/"+xingE);
        }
        return ResultEntity.success("Get Passenger Info Success",passengeList);
    }

    /**
     * 添加乘客
     * @param flyPassenge
     * @return
     */
    @Override
    public ResultEntity addPassengerInfo(FlyFrequentPassenger flyPassenge) {
        if (flyPassenge!=null){
            flyPassenge.setId(Long.valueOf(IDUtils.genItemId()));
            flyPassenge.setSetDate(new Date());
            flyPassenge.setName(flyPassenge.getMingE()+"/"+flyPassenge.getXingE());
            flyPassenge.setUpdateDate(new Date());
        }
        flyPassenge.setIsFlightsms(0);
        int i = flyFrequentPassengerMapper.insertSelective(flyPassenge);
        return i==1?ResultEntity.success("Add Passenger Success"):ResultEntity.fail("Add Passenger Failed");
    }

    @Override
    public ResultEntity deleteFrequentFlyer(Long id, String userId) {
        FlyFrequentPassenger  flyPassenge = flyFrequentPassengerMapper.selectByPrimaryKey(id);
        if(flyPassenge.getUserId().equals(userId)){
            flyPassenge.setIsFlightsms(1);
            int num = flyFrequentPassengerMapper.updateByPrimaryKey(flyPassenge);
            return num>=0? ResultEntity.success("delete success"): ResultEntity.fail("delete failure");
        }
        return ResultEntity.fail("delete failure");
    }


}
